package com.wu.project.admin.service.impl;

import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.util.RandomUtil;
import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.dao.UserDao;
import com.wu.project.admin.entity.*;
import com.wu.project.admin.service.UserService;
import com.wu.project.admin.util.CommonResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author xuan
 */
@Service
public class UserServiceImpl implements UserService {

    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    @Autowired
    UserDao userDao;

    @Override
    public JsonResult getUserList(UserQueryVO userQueryVO) {
        Integer offset = CommonResultUtil.Instance.getOffset(userQueryVO.getPageNum(), userQueryVO.getPageSize());
        userQueryVO.setOffset(offset);

        List<UserFullDO> userList = userDao.queryUserList(userQueryVO);
        Integer total = userDao.queryUserListSize(userQueryVO);
        PageData<List<UserFullDO>> pageData = new PageData<>();
        pageData.setTotal(total);
        pageData.setPageSize(userQueryVO.getPageSize());
        pageData.setPageNum(userQueryVO.getPageNum());
        pageData.setData(userList);
        return CommonResultUtil.Instance.success(pageData);
    }

    @Override
    public JsonResult getWorkerList(WorkerQueryVO workerQueryVO) {
        Integer offset = CommonResultUtil.Instance.getOffset(workerQueryVO.getPageNum(), workerQueryVO.getPageSize());
        workerQueryVO.setOffset(offset);

        List<UserWorkerDO> workerList = userDao.queryWorkerList(workerQueryVO);
        Integer total = userDao.queryWorkerListSize(workerQueryVO);
        PageData<List<UserWorkerDO>> pageData = new PageData<>();
        pageData.setTotal(total);
        pageData.setPageSize(workerQueryVO.getPageSize());
        pageData.setPageNum(workerQueryVO.getPageNum());
        pageData.setData(workerList);
        return CommonResultUtil.Instance.success(pageData);
    }

    @Override
    public JsonResult addUser(UserFullDO userDTO) {
        Integer userLength = userDao.checkUserName(userDTO.getUserName());

        if (userLength > 0) {
            return CommonResultUtil.Instance.error();
        }
        String randomSalt = RandomUtil.randomString(6);
        userDTO.setUserSaltvalue(randomSalt);
        userDTO.setUserCreatime(new Date());
        Integer integer = userDao.addUser(userDTO);
        if (integer > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult updateUser(UserFullDO userDTO) {
        Integer integer = userDao.updateUser(userDTO);
        if (integer > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult delUser(UserFullDO userDTO) {
        Integer integer = userDao.delUser(userDTO);
        if (integer > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult addWorker(UserWorkerDO workerDTO) {
        Integer count = userDao.checkWorkerName(workerDTO.getWorkerName());
        if (count > 0) {
            return CommonResultUtil.Instance.error();
        }

        String randomSalt = RandomUtil.randomString(6);
        workerDTO.setUserSaltvalue(randomSalt);
        String code = RandomUtil.randomString(6);
        workerDTO.setWorkerInvite(code);
        workerDTO.setWorkerCretime(new Date());
        Integer integer = userDao.addWorker(workerDTO);
        if (integer > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult updateWorker(UserWorkerDO workerDTO) {
        Integer integer = userDao.updateWorker(workerDTO);
        if (integer > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.success();
    }

    @Override
    public JsonResult delWorker(UserWorkerDO workerDTO) {
        Integer integer = userDao.delWorker(workerDTO);
        if (integer > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult queryUserList() {
        return CommonResultUtil.Instance.success(userDao.userSimpleList());
    }
}
